package com.mengchengnet.chaperonage.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.adapter.BaseInfoAdapter;
import com.mengchengnet.chaperonage.adapter.ChaperonageAdapter;
import com.mengchengnet.chaperonage.domain.base.BaseInfo;
import com.mengchengnet.chaperonage.domain.base.Chaperonage;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;
import com.mengchengnet.chaperonage.util.ViewUtil;
import com.mengchengnet.chaperonage.view.DropDownMenu;
import com.mengchengnet.chaperonage.view.refreshableListView.RefreshableListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/14.
 */

public class CareListActivity extends BaseActivity {
    private String title;
    private DropDownMenu ddmPrice;
    private DropDownMenu ddmAge;
    private DropDownMenu ddmSort;
    private DropDownMenu ddmSex;

    private RefreshableListView rlvChaperonage;
    private ChaperonageAdapter chaperonageAdapter;
    private ArrayList<Chaperonage> chaperonageList;
    private TextView tv_location;
    private String location;
    private RequestQueue requestQueue;
    private static final String TAG = "CareListActivity";
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            chaperonageAdapter = new ChaperonageAdapter(getApplicationContext(), R.layout.item_chaperonage_listview, chaperonageList);
            rlvChaperonage.setPullLoadEnable(false);
            rlvChaperonage.setPullRefreshEnable(false);
            rlvChaperonage.setAdapter(chaperonageAdapter);
            rlvChaperonage.setRefreshableListViewListener(new RefreshableListView.RefreshableListViewListener() {
                @Override
                public void onRefresh() {

                }

                @Override
                public void onLoadMore() {

                }
            });
            rlvChaperonage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String name=chaperonageList.get(position-1).getName();
                    String address=chaperonageList.get(position-1).getHometown();
                    String age=chaperonageList.get(position-1).getAge();
                    String workAge=chaperonageList.get(position-1).getWorkTime();
                    String head=chaperonageList.get(position-1).getPhoto();
                    String evaulate=chaperonageList.get(position-1).getDescription();
                    String price=chaperonageList.get(position-1).getPrice();
                    String careId=chaperonageList.get(position-1).getChaperonageId();
                    String commentCount=chaperonageList.get(position-1).getCommentsCount();
                    int starCount=chaperonageList.get(position-1).getRating();

                    Intent intent = new Intent(BaseActivity.currentActivity, OrderActivity.class);
                    intent.putExtra("name",name);
                    intent.putExtra("address",address);
                    intent.putExtra("age",age);
                    intent.putExtra("workAge",workAge);
                    intent.putExtra("head",head);
                    intent.putExtra("evaulate",evaulate);
                    intent.putExtra("price",price);
                    intent.putExtra("careId",careId);
                    intent.putExtra("commentCount",commentCount);
                   intent.putExtra("star",starCount);
                    startActivity(intent);
                }
            });
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_care_list);
        Intent intent=getIntent();
        title=intent.getStringExtra("name");
        initView();
        getList();

    }

    private void initView() {
        requestQueue= Volley.newRequestQueue(getApplication());
        location= PreferenceUtil.getString(this,"location");
        tv_location= (TextView) findViewById(R.id.careList_tv_location);
        if (!location.isEmpty()){
            tv_location.setText(location);
        }
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("护工选择");
        ddmSort= (DropDownMenu) findViewById(R.id.careList_type);
        ddmAge= (DropDownMenu) findViewById(R.id.careList_age);
        ddmPrice= (DropDownMenu) findViewById(R.id.careList_price);
        ddmSex= (DropDownMenu) findViewById(R.id.careList_sex);

        List<BaseInfo> priceList = new ArrayList<>();
        priceList.add(new BaseInfo("1", "0元~99元"));
        priceList.add(new BaseInfo("2", "100元~199元"));
        priceList.add(new BaseInfo("3", "200元以上"));

        List<BaseInfo> ageList = new ArrayList<>();
        ageList.add(new BaseInfo("1", "20岁~29岁"));
        ageList.add(new BaseInfo("2", "30岁~39岁"));
        ageList.add(new BaseInfo("3", "40岁以上"));

        List<BaseInfo> sortList = new ArrayList<>();
        sortList.add(new BaseInfo("0", "陪护师"));
        sortList.add(new BaseInfo("1", "月嫂"));
        sortList.add(new BaseInfo("2", "育婴师"));
        sortList.add(new BaseInfo("3", "催乳师"));

        List<BaseInfo> sexList = new ArrayList<>();
        sexList.add(new BaseInfo("0", "男"));
        sexList.add(new BaseInfo("1", "女"));

        final BaseInfoAdapter priceAdapter = new BaseInfoAdapter(this, R.layout.list_item, priceList);
        ddmPrice.setAdapter(priceAdapter);
        ddmPrice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e(TAG, "onItemClick点击的位置: "+position);
            }
        });


        final BaseInfoAdapter ageAdapter = new BaseInfoAdapter(this, R.layout.list_item, ageList);
        ddmAge.setAdapter(ageAdapter);
        ddmAge.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        final BaseInfoAdapter sortAdapter = new BaseInfoAdapter(this, R.layout.list_item, sortList);
        ddmSort.setAdapter(sortAdapter);
        if(title.equals("陪护师")){
            ddmSort.setDefaultItem(sortList.get(0));
        }else if(title.equals("月嫂")){
            ddmSort.setDefaultItem(sortList.get(1));
        }else if(title.equals("育婴师")){
            ddmSort.setDefaultItem(sortList.get(2));
        }else if(title.equals("催乳师")){
            ddmSort.setDefaultItem(sortList.get(3));
        }

        ddmSort.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        final BaseInfoAdapter busyAdapter = new BaseInfoAdapter(this, R.layout.list_item, sexList);
        ddmSex.setAdapter(busyAdapter);
        ddmSex.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        chaperonageList = new ArrayList<>();
        rlvChaperonage = (RefreshableListView) findViewById(R.id.careList_listView);

    }


    private void getList() {
        ViewUtil.showProgressDialog("加载中...");
        StringRequest request = new StringRequest(Request.Method.POST, Url.GetCareList,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e(TAG, "获取陪护人员列表返回值："+result );
                        JSONObject jsonObject;
                        try {
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                ViewUtil.hideProgressDialog();
                                JSONArray array=jsonObject.getJSONArray("careList");
                                for (int i = 0; i <array.length(); i++) {
                                    JSONObject obj=array.getJSONObject(i);

                                    String careName=obj.getString("careName");
                                    String careId=obj.getString("careId");
                                    String careDistance=obj.getString("careDistance");
                                    String brief=obj.getString("brief");
                                    String carePrice=obj.getString("carePrice");
                                    String careIcon=obj.getString("careIcon");
                                    String careHome=obj.getString("careHome");
                                    String careAge=obj.getString("careAge");
                                    String careWorkAge=obj.getString("careWorkAge");
                                    String commentCount=obj.getString("dp_count");
                                    int avg_point=obj.getInt("avg_point");

                                    Chaperonage chaperonage = new Chaperonage();
                                    chaperonage.setAge(careAge);
                                    chaperonage.setDescription(brief);
                                    chaperonage.setDistance(careDistance);
                                    chaperonage.setHometown(careHome);
                                    chaperonage.setName(careName);
                                    chaperonage.setPhoto(careIcon);
                                    chaperonage.setPrice(carePrice);
                                    chaperonage.setRating(avg_point);
                                    chaperonage.setWorkTime(careWorkAge);
                                    chaperonage.setChaperonageId(careId);
                                    chaperonage.setCommentsCount(commentCount);
                                    chaperonageList.add(chaperonage);

                                }
                                handler.sendEmptyMessage(0);

                            }
                            else {
                                Toast.makeText(CareListActivity.this, "陪护列表获取失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(CareListActivity.this, "网络异常请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("version", PreferenceUtil.getString(getApplication(),"versionCode"));
                map.put("personType", "0");
                map.put("latitude", PreferenceUtil.getString(getApplication(),"Latitude"));
                map.put("longitude", PreferenceUtil.getString(getApplication(),"Longitude"));
                map.put("price", "0");
                map.put("workAge", "0");
                map.put("sex", "0");
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }


}
